package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-12-01
 */
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    HospitalSetService hospitalSetService;


    @ApiOperation(value = "设置医院状态")
    @PostMapping("lockHospSet/{id}/{status}")
    public Result lockHospSet(@PathVariable Long id,
                              @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        hospitalSet.setStatus(status);

        hospitalSetService.updateById(hospitalSet);

        return Result.ok();
    }


    @ApiOperation("批量删除")
    @DeleteMapping("/removeHospSet")
    public Result removeHospSet(@RequestBody List<Long> list) {
        hospitalSetService.removeByIds(list);
        return Result.ok();

    }

    @ApiOperation(value = "根据id修改医院设置")
    @PostMapping("/updateHospSet")
    public Result updateHospSet(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }


    @ApiOperation(value = "根据id查医院设置")
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok().data("hospital", hospitalSet);
    }


    @ApiOperation(value = "添加医院")
    @PostMapping("/saveHosp")
    public Result saveHosp(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.save(hospitalSet);
        return Result.ok();
    }


    @ApiOperation(value = "条件查询+分页")
    @PostMapping("/hospSetPageQuery/{current}/{limit}")
    public Result hospSetPageQuery(@PathVariable Long current,
                                   @PathVariable Long limit,
                                   @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> page = new Page<>(current, limit);
        if (hospitalSetQueryVo == null) {
            hospitalSetService.page(page, null);
        } else {
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode", hoscode);
            }
            if (!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname", hoscode);
            }
            hospitalSetService.page(page, wrapper);
        }

        List<HospitalSet> list = page.getRecords();

        long total = page.getTotal();
        return Result.ok().data("total", total).data("rows", list);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/findPage/{current}/{limit}")
    public Result findPage(@PathVariable Long current,
                           @PathVariable Long limit) {
        Page<HospitalSet> page = new Page<>(current, limit);
        hospitalSetService.page(page, null);
        List<HospitalSet> list = page.getRecords();
        long total = page.getTotal();
        return Result.ok().data("total", total).data("rows", list);
    }


    @ApiOperation(value = "医院设置列表")
    @GetMapping("/findAll")
    public Result findAll() {
        //int a = 1 / 0;
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok().data("items", list);
    }


    @ApiOperation(value = "删除医院设置")
    @DeleteMapping("/remove/{id}")
    public Result removeById(@PathVariable String id) {
        hospitalSetService.removeById(id);
        return Result.ok();
    }
}

